<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>Shared Worker 示例</title>
</head>

<body>
    <h1>Shared Worker 示例</h1>
    <input type="text" id="sharedInput" placeholder="输入消息">
    <button id="sendToWorker">发送消息</button>
    <p id="sharedResult"></p>

    <script>
        const sharedWorker = new SharedWorker("shared.js");

        // onmessage = ()=>{} 默认开启了sharedWorker.port.start
        sharedWorker.port.onmessage = event => {
            console.log(event.data)
        };

        document.getElementById("sendToWorker").onclick = () => {
            const message = document.getElementById("sharedInput").value;
            sharedWorker.port.postMessage(message);
        };
    </script>
</body>

</html>